package cn.yunhe.servlet;

import cn.yunhe.entity.merchantsentity.Food;
import cn.yunhe.entity.merchantsentity.MerchantsAccount;
import cn.yunhe.entity.merchantsentity.MerchantsData;
import cn.yunhe.entity.merchantsentity.Type;
import cn.yunhe.services.AddFoodService;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * 周依博
 *
 * 菜品添加的servlet
 * @param
 * @return
 */
@WebServlet("/shangjia/AddFoodServlet")
public class AddFoodServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SmartUpload sm  =new SmartUpload();

        try {
            sm.initialize(this.getServletConfig(),request,response);
            sm.setCharset("UTF-8");
            sm.upload();
            sm.save("upfiles");
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Request suRequest = sm.getRequest();
        String name = suRequest.getParameter("name");
        String price = suRequest.getParameter("price");
        int typeid = Integer.parseInt(suRequest.getParameter("typeid"));
        String pic = sm.getFiles().getFile(0).getFileName();

        HttpSession session = request.getSession();
        int mid= (int) session.getAttribute("mid");
        Map map = new HashMap();
        map.put("name",name);
        map.put("mid",mid);
        AddFoodService addFoodService = new AddFoodService();
        if (addFoodService.searchFood(map)>0){
            out.print("<script>alert('已经有了这道菜！');location.href='ShowTypeServlet'</script>");
        }else {
            Food food = new Food();

            if (name.equals("")||price.equals("")||pic.equals("")){

                out.print("<script>alert('老弟，你是不是少了点东西!');location.href='/shangjia/ShowTypeServlet'</script>");
            }else {
                food.setName(name);
                food.setPrice(price);
                food.setPic(pic);
                Type type = new Type();
                type.setTid(typeid);
                food.setType(type);
                MerchantsAccount ma = new MerchantsAccount();
                ma.setMid(mid);
                MerchantsData m = new MerchantsData();
                m.setMerAccount(ma);
                food.setMerData(m);
                if (addFoodService.addFood(food)>0){

                    out.print("<script>alert('成功');location.href='/shangjia/product-list.jsp'</script>");
                }
            }
        }



    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
